//
//  HomePageViewController.m
//  Logistics
//
//  Created by 华谕 on 2017/10/30.
//  Copyright © 2017年 HUYU. All rights reserved.
//

#import "HomePageViewController.h"
#import <TencentLBS/TencentLBS.h>
#import "SDCycleScrollView.h"
#import "BannerModel.h"
#import "H5PublicViewController.h"
#import "RegisterViewController.h"
#import "CloseCityViewController.h"
#import "HYNavBaseViewController.h"
#import "CloseAreaViewController.h"
#import "QueryShuttleViewController.h"
#import "MoreServiceModel.h"
#import "SearchComPanyViewController.h"
#import "FreightQueryViewController.h"
#import "SuppHallViewController.h"
#import "AddressBookViewController.h"
#import "LogisticsCircleViewController.h"
#import "LoginViewController.h"

@interface HomePageViewController ()<TencentLBSLocationManagerDelegate,UIScrollViewDelegate,SDCycleScrollViewDelegate>
{
    UIButton *cityBtn;
    UIImageView *searimgView;
    UILabel *searLab;
    UILabel *deliveryLab;
    UILabel *goodsLab;
    UILabel *lookLab;
    UIScrollView *backView;
    CGFloat W;
    CGFloat HEIGHT;
    UIView *moreView;
}
@property (nonatomic, strong) TencentLBSLocationManager *locationManager;
@property (nonatomic, strong)SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong)NSString *cityId;
@property (nonatomic, strong)NSArray *imagesAry;
@property (nonatomic, strong)NSString *Citynews;
@property (nonatomic, strong)NSString *deliverCity;//发货城市
@property (nonatomic, strong)NSString *deliverId;
@property (nonatomic, strong)NSString *goodsCity;//收货城市
@property (nonatomic, strong)NSString *goodsId;
@property (nonatomic, strong)NSString *latitude;
@property (nonatomic, strong)NSString *longitude;
@property (nonatomic, strong)NSArray *moreServiAry;

@end

@implementation HomePageViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(callPhonesNotifice:) name:@"callPhones" object:nil];
    [_cycleScrollView adjustWhenControllerViewWillAppera];
    
    [self requestHomeBanner:_cityId];
    [self requestQueryCount];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"callPhones" object:nil];
}


- (void)callPhonesNotifice:(NSNotification *)center
{
    NSString *timeStr = [center object];
    Student *stu = [Student studentWith:@"" name:@"专线网" Phone:@"075589301856" Times:timeStr];
    BOOL isInsert = [StudentFMDNManager addStudent:stu];
    
    if (isInsert) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入失败");
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _cityId = @"-1";
    _Citynews = @"";
    _latitude = @"";
    _longitude = @"";
    _deliverId = @"28558";
    [self creatUI];
    [self configLocationManager];
    [self startSingleLocation];
    
}

- (void)creatUI
{
    self.navigationItem.title = @"专线网";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    backView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-STA_NAV_HEIGHT-TABBAR_HEIGHT - SCREER_HeiX)];
    backView.backgroundColor = [UIColor whiteColor];
    backView.delegate = self;
    backView.contentOffset = CGPointMake(0, 0);
    backView.pagingEnabled = NO;
    backView.bounces = NO;
//    backView.showsVerticalScrollIndicator = YES;
    [self.view addSubview:backView];
    W = SCREEN_WIDTH-30*displaywidth;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44*displayheight)];
    topView.backgroundColor = ThemeColor;
    [backView addSubview:topView];
    UIView *searView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 7*displayheight, SCREEN_WIDTH-30*displaywidth, 30*displayheight)];
    searView.layer.cornerRadius = 5;
    searView.layer.masksToBounds = YES;
    searView.backgroundColor = RGB(43, 73, 141);
    [topView addSubview:searView];
    BOOL isLogin = [LTools storageForKey:SERVER_IsLogin];
    if (!isLogin)
    {
        W = SCREEN_WIDTH-115*displaywidth;
        searView.frame = CGRectMake(15*displaywidth, 7*displayheight, SCREEN_WIDTH-115*displaywidth, 30*displayheight);
        UIButton *regisBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH - 85*displaywidth, 7*displayheight, 70*displaywidth, 30*displayheight) ImageName:@"sy_icon05" Target:self Action:@selector(regisAction) Title:@" 入驻" Font:13.0];
        regisBtn.backgroundColor = RGB(61, 89, 151);
        regisBtn.layer.cornerRadius = 5;
        regisBtn.layer.masksToBounds = YES;
        [topView addSubview:regisBtn];
    }
    cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cityBtn.backgroundColor =  RGB(61, 89, 151);
    cityBtn.frame = CGRectMake(0, 0, 70*displaywidth, 30*displayheight);
    [cityBtn setImage:[UIImage imageNamed:@"zhankai"] forState:UIControlStateNormal];
    cityBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [cityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cityBtn setTitle:@"深圳" forState:UIControlStateNormal];
    [cityBtn addTarget:self action:@selector(closeCity) forControlEvents:UIControlEventTouchUpInside];
    CGSize imgSize = cityBtn.currentImage.size;
    CGSize titleSize = [cityBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:cityBtn.titleLabel.font}];
    UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
    UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
    cityBtn.titleEdgeInsets = titleEdgeInsets;
    cityBtn.imageEdgeInsets = imgEdgeInsets;
    CGFloat cityWidth = imgSize.width+titleSize.width+20*displaywidth;
    cityBtn.frame = CGRectMake(0, 0, cityWidth, 30*displayheight);
    [searView addSubview:cityBtn];
    searimgView = [ZCControl createImageViewWithFrame:CGRectMake(W-25*displaywidth, 5*displayheight, 20*displaywidth, 20*displayheight) ImageName:@"sy_icon04"];
    searimgView.contentMode = UIViewContentModeScaleAspectFit;
    [searView addSubview:searimgView];
    searLab = [ZCControl createLabelWithFrame:CGRectMake(cityWidth, 0, W-cityWidth-25*displaywidth, 30*displayheight) Font:13.0 Text:@"公司信誉查询" Color:[UIColor whiteColor]];
    searLab.textAlignment = NSTextAlignmentCenter;
    [searView addSubview:searLab];
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)];
    [searLab addGestureRecognizer:labelTapGestureRecognizer];
    searLab.userInteractionEnabled = YES;
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 44*displayheight, SCREEN_WIDTH, 180*displayheight) delegate:self placeholderImage:nil];
    _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _cycleScrollView.delegate = self;
    //是否自动轮播
    _cycleScrollView.autoScroll = YES;//是否自动轮播 bannerImageViewContentMode
    //图片样式
    _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    _cycleScrollView.clipsToBounds = YES;
    //分页样式
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    [backView addSubview:_cycleScrollView];
    
    CGFloat H = 250*displayheight;
    UIButton *deliveryBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, H, 120*displaywidth, 15*displayheight) ImageName:@"sy_icon01" Target:self Action:@selector(deliveryAction) Title:@" 发货城市" Font:13.0];
    [deliveryBtn setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
    deliveryBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    deliveryBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [backView addSubview:deliveryBtn];
    
    UIButton *goodsBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH- 135*displaywidth, H, 120*displaywidth, 15*displayheight) ImageName:@"sy_icon02" Target:self Action:@selector(goodsAction) Title:@" 收货城市" Font:13.0];
    [goodsBtn setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
    goodsBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    goodsBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [backView addSubview:goodsBtn];
    
    UIImageView *contImgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH/2-20*displayheight, H + 20*displayheight, 40*displayheight, 40*displayheight) ImageName:@"sy_icon03"];
    contImgView.layer.cornerRadius = 20*displayheight;
    contImgView.layer.masksToBounds = YES;
    [backView addSubview:contImgView];
    
    deliveryLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, H+25*displayheight, SCREEN_WIDTH/2-40*displaywidth, 40*displayheight) Font:30.0 Text:@"深圳" Color:RGB(48, 48, 48)];
    deliveryLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0];
    [backView addSubview:deliveryLab];
    goodsLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH/2+ 25*displaywidth, H+25*displayheight, SCREEN_WIDTH/2-40*displaywidth, 40*displayheight) Font:30.0 Text:@"请选择" Color:RGB(48, 48, 48)];
    goodsLab.textAlignment = NSTextAlignmentRight;
    goodsLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0];
    [backView addSubview:goodsLab];
    
    UIButton *leftButton = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, H+25*displayheight, SCREEN_WIDTH/2-40*displaywidth, 40*displayheight) ImageName:nil Target:self Action:@selector(deliveryAction) Title:nil Font:12.0];
    leftButton.backgroundColor = [UIColor clearColor];
    [backView addSubview:leftButton];
    UIButton *rightButton = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2+ 25*displaywidth, H+25*displayheight, SCREEN_WIDTH/2-40*displaywidth, 40*displayheight) ImageName:nil Target:self Action:@selector(goodsAction) Title:nil Font:12.0];
    rightButton.backgroundColor = [UIColor clearColor];
    [backView addSubview:rightButton];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, H+78*displayheight, SCREEN_WIDTH/2-45*displaywidth, 1)];
    leftView.backgroundColor = RGB(225, 225, 225);
    [backView addSubview:leftView];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2+ 30*displaywidth, H+78*displayheight, SCREEN_WIDTH/2-45*displaywidth, 1)];
    rightView.backgroundColor = RGB(225, 225, 225);
    [backView addSubview:rightView];
    
    lookLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, H+110*displayheight, SCREEN_WIDTH-30*displaywidth, 15*displayheight) Font:12.0 Text:@"" Color:RGB(153,153,153)];
    lookLab.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:lookLab];
    
    UIButton *queryBtn = [ZCControl createButtonWithFrame:CGRectMake(30*displaywidth, H+130*displayheight, SCREEN_WIDTH-60*displaywidth, 44*displayheight) ImageName:nil Target:self Action:@selector(queryAction) Title:@"专线查询" Font:18.0];
    queryBtn.backgroundColor = RGB(255, 153, 20);
    [queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queryBtn.layer.cornerRadius = 22*displayheight;
    queryBtn.layer.masksToBounds = YES;
    [backView  addSubview:queryBtn];
    NSArray *imgAry = @[@"sy_icon0611",@"sy_icon0711",@"sy_icon0811",@"sy_icon0911"];
    NSArray *nameAry = @[@"货源大厅",@"运价查询",@"物流圈子",@"常用物流"];
    CGFloat itemX = (SCREEN_WIDTH-270*displaywidth)/3;
    for (int i = 0; i <  imgAry.count; i++)
    {
        UIButton *item = [ZCControl createButtonWithFrame:CGRectMake(35*displaywidth+(itemX+50*displaywidth)*i, H+200*displayheight, 50*displaywidth, 50*displaywidth) ImageName:imgAry[i] Target:self Action:@selector(typeCloseAction:) Title:nil Font:12.0];
        item.tag = 100+i;
        [backView addSubview:item];
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(25*displaywidth+(itemX+50*displaywidth)*i, H+255*displayheight, 70*displaywidth, 20*displayheight) Font:13.0 Text:nameAry[i] Color:RGB(51, 51, 51)];
        nameLab.textAlignment = NSTextAlignmentCenter;
        [backView addSubview:nameLab];
        
    }
    HEIGHT = H+300*displayheight;
    moreView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT, SCREEN_WIDTH, 200*displaywidth)];
    moreView.backgroundColor = BacksColor;
    [backView addSubview:moreView];
    backView.contentSize = CGSizeMake(SCREEN_WIDTH, HEIGHT+200*displaywidth);

}


//公司信誉查询
- (void)labelClick
{
    SearchComPanyViewController *vc = [[SearchComPanyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)typeCloseAction:(UIButton *)btn
{
    NSInteger index = btn.tag-100;
    if (index == 0)
    {
        //货源大厅
        SuppHallViewController *vc = [[SuppHallViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (index == 1)
    {
        //运价查询
        FreightQueryViewController *vc = [[FreightQueryViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];

    }
    else if (index == 2)
    {
        //物流圈子
        if (![LTools storageForKey:SERVER_IsLogin])
        {
            [LTools showMBProgressWithText:@"请先登录" addToView:sharedDelegate.window];
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            //nav化
            HYNavBaseViewController *navVC = [[HYNavBaseViewController alloc] initWithRootViewController:loginVC];
            [self presentViewController:navVC animated:YES completion:nil];
            return;
        }
        LogisticsCircleViewController *circleVC = [[LogisticsCircleViewController alloc] init];
        circleVC.circleType = ALLTYPE;
        [self.navigationController pushViewController:circleVC animated:YES];
    }
    else
    {
        //常用物流
        AddressBookViewController *vc = [[AddressBookViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//选择发货城市
- (void)deliveryAction
{
    CloseAreaViewController *vc = [[CloseAreaViewController alloc] init];
    vc.areaType = 1;
    WEAKSELF;
    vc.closeBlock = ^(NSString *city, NSString *areaId)
    {
        weakSelf.deliverCity = city;
        weakSelf.deliverId = areaId;
        dispatch_async(dispatch_get_main_queue(), ^{
            deliveryLab.text = city;
        });

    };
    [self.navigationController pushViewController:vc animated:YES];
    
}

//选择收货城市
- (void)goodsAction
{
    CloseAreaViewController *vc = [[CloseAreaViewController alloc] init];
    vc.areaType = 2;
    vc.shipmentId = _deliverId;
    WEAKSELF;
    vc.closeBlock = ^(NSString *city, NSString *areaId)
    {
        weakSelf.goodsCity = city;
        weakSelf.goodsId = areaId;
        dispatch_async(dispatch_get_main_queue(), ^{
            goodsLab.text = city;
        });
        
    };
    [self.navigationController pushViewController:vc animated:YES];
}

//专线查询
- (void)queryAction
{
    if (_goodsId.length == 0 || _goodsCity.length == 0)
    {
        [LTools showMBProgressWithText:@"请选择收货城市" addToView:self.view];
        return;
    }
    QueryShuttleViewController *vc = [[QueryShuttleViewController alloc] init];
    vc.startCity = _deliverCity;
    vc.stopCity = _goodsCity;
    vc.startCityId = _deliverId;
    vc.stopCityId = _goodsId;
    vc.latStr = _latitude;
    vc.lngStr = _longitude;
    [self.navigationController pushViewController:vc animated:YES];

}

//入驻
- (void)regisAction
{
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//选择位置
- (void)closeCity
{
    CloseCityViewController *vc = [[CloseCityViewController alloc] init];
    vc.cityStr = _Citynews;
    WEAKSELF;
    vc.closeBlock = ^(NSString *namestr, NSString *cityID) {
        weakSelf.deliverCity = namestr;
        weakSelf.deliverId = cityID;
        [cityBtn setTitle:namestr forState:UIControlStateNormal];
        deliveryLab.text = namestr;
        CGSize imgSize = cityBtn.currentImage.size;
        CGSize titleSize = [cityBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:cityBtn.titleLabel.font}];
        UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
        UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
        cityBtn.titleEdgeInsets = titleEdgeInsets;
        cityBtn.imageEdgeInsets = imgEdgeInsets;
        CGFloat cityWidth = imgSize.width+titleSize.width+20*displaywidth;
        cityBtn.frame = CGRectMake(0, 0, cityWidth, 30*displayheight);
        searLab.frame = CGRectMake(cityWidth, 0, W-cityWidth-25*displaywidth, 30*displayheight);
        [self requestHomeBanner:_deliverId];
        [weakSelf requestMoreServices];
    };
    HYNavBaseViewController *navVC = [[HYNavBaseViewController alloc] initWithRootViewController:vc];
    [self presentViewController:navVC animated:YES completion:nil];
}

- (void)configLocationManager
{
    self.locationManager = [[TencentLBSLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    [self.locationManager setApiKey:TenxunMapKey];
    
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
    // 需要后台定位的话，可以设置此属性为YES。
    [self.locationManager setAllowsBackgroundLocationUpdates:NO];
    
    // 如果需要POI信息的话，根据所需要的级别来设定，定位结果将会根据设定的POI级别来返回，如：
    [self.locationManager setRequestLevel:TencentLBSRequestLevelName];
    
    // 申请的定位权限，得和在info.list申请的权限对应才有效
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    if (authorizationStatus == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    
}

// 单次定位
- (void)startSingleLocation {
    WEAKSELF;
    [self.locationManager requestLocationWithCompletionBlock:
     ^(TencentLBSLocation *location, NSError *error) {
         NSLog(@"%@, %@, %@", location.location, location.name, location.address);
         NSString *name = location.city ? location.city : @"";
         weakSelf.latitude = [NSString stringWithFormat:@"%f",location.location.coordinate.latitude];
         weakSelf.longitude = [NSString stringWithFormat:@"%f",location.location.coordinate.longitude];
         [LTools cache:location.address ForKey:SERVER_Address];
         [LTools cache:location.city ForKey:SERVER_City];
         [LTools cache:weakSelf.latitude ForKey:SERVER_latitude];
         [LTools cache:weakSelf.longitude ForKey:SERVER_longitude];

         if (name.length > 1)
         {
             NSDictionary *dic = @{@"name":[name substringToIndex:[name length] - 1],
                                   @"level":@"2"
                                   };
             [weakSelf requestAreaList:dic];

         }
     }];
}


- (void)tencentLBSLocationManager:(TencentLBSLocationManager *)manager
                 didFailWithError:(NSError *)error {
    WEAKSELF;
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    if (authorizationStatus == kCLAuthorizationStatusDenied ||
        authorizationStatus == kCLAuthorizationStatusRestricted) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                       message:@"定位权限未开启，是否开启？"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"是"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
                                                    if( [[UIApplication sharedApplication]canOpenURL:
                                                         [NSURL URLWithString:UIApplicationOpenSettingsURLString]] ) {
                                                        [[UIApplication sharedApplication] openURL:
                                                         [NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                                                    }
                                                }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"否"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
                                                }]];
        
        [self presentViewController:alert animated:true completion:nil];
        
    } else {
        [LTools showMBProgressWithText:[NSString stringWithFormat:@"%@", error] addToView:weakSelf.view];
    }
}




- (void)requestHomeBanner:(NSString *)AreaId
{
    WEAKSELF;
    NSDictionary *dic = @{@"city.id":AreaId};
    [HYBaseHandler requestHomeBannerParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            _imagesAry = [BannerModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:0];
            for (BannerModel *model in _imagesAry)
            {
                [imgArray addObject:model.img];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.cycleScrollView.imageURLStringsGroup = imgArray;
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
    
    }];
}

- (void)requestQueryCount
{
    WEAKSELF;
    [HYBaseHandler requestQueryCountParmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString *countStr = [obj objectForKey:@"data"];
                lookLab.text = [NSString stringWithFormat:@"累计查询：%@人次",countStr];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

//定位匹配
- (void)requestAreaList:(NSDictionary *)dic
{
    WEAKSELF;
    [HYBaseHandler requestAreaListParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSArray *dataAry = obj[@"data"];
                if (dataAry.count > 0)
                {
                    NSDictionary *dataDic = dataAry[0];
                    NSString *namestr = dataDic[@"name"];
                    weakSelf.Citynews = namestr;
                    [LTools cache:namestr ForKey:SERVER_CityNew];
                    _deliverId = [NSString stringWithFormat:@"%@",dataDic[@"id"]];
                    [LTools cache:[NSString stringWithFormat:@"%@",dataDic[@"id"]] ForKey:SERVER_CityiDNew];
                    [cityBtn setTitle:namestr forState:UIControlStateNormal];
                    deliveryLab.text = namestr;
                    _deliverCity = namestr;
                    CGSize imgSize = cityBtn.currentImage.size;
                    CGSize titleSize = [cityBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:cityBtn.titleLabel.font}];
                    UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
                    UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
                    cityBtn.titleEdgeInsets = titleEdgeInsets;
                    cityBtn.imageEdgeInsets = imgEdgeInsets;
                    CGFloat cityWidth = imgSize.width+titleSize.width+20*displaywidth;
                    cityBtn.frame = CGRectMake(0, 0, cityWidth, 30*displayheight);
                    searLab.frame = CGRectMake(cityWidth, 0, W-cityWidth-25*displaywidth, 30*displayheight);
                    [weakSelf requestMoreServices];
                    [weakSelf requestHomeBanner:_deliverId];
                }
                
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"点击了第%ld张",index);
    BannerModel *model = _imagesAry[index];
    if ([model.url containsString:@"http"])
    {
        H5PublicViewController *vc = [[H5PublicViewController alloc] init];
        vc.url = model.url;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

- (void)requestMoreServices
{
    WEAKSELF;
    NSDictionary *dic = @{@"cityId":_deliverId};
    [HYBaseHandler requestWithTheFormUrl:SERVER_homeFindByCity Parmers:dic Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            weakSelf.moreServiAry = [MoreServiceModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf creatMoreServicesView];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)creatMoreServicesView
{
    for (id v in moreView.subviews) {
        [v removeFromSuperview];
    }
    UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 15*displaywidth, SCREEN_WIDTH-30*displaywidth, 35*displaywidth) Font:15.0 Text:@"更多专业服务" Color:ThemeColor];
    titLab.textAlignment = NSTextAlignmentCenter;
    [moreView addSubview:titLab];
    CGFloat H = 50*displaywidth;
    if (_moreServiAry.count > 0)
    {
        NSInteger num = _moreServiAry.count/2;
        if (_moreServiAry.count %2 != 0)
        {
            num+=1;
        }
        H = 50*displaywidth+num*60*displaywidth;
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 50*displaywidth, SCREEN_WIDTH-30*displaywidth, num*60*displaywidth)];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.layer.cornerRadius = 10;
        whiteView.layer.masksToBounds = YES;
        [moreView addSubview:whiteView];
        CGFloat windth = (SCREEN_WIDTH-30*displaywidth)/2;
        UIView *lineAView = [[UIView alloc] initWithFrame:CGRectMake(windth-0.5*displaywidth, 0, 1*displaywidth, num*60*displaywidth)];
        lineAView.backgroundColor = BacksColor;
        [whiteView addSubview:lineAView];
        for (int i = 0; i < _moreServiAry.count; i++)
        {
            MoreServiceModel *model = _moreServiAry[i];
            UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(windth*(i%2)+30*displaywidth, 60*displaywidth*(i/2)+15*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:nil];
            [imgView sd_setImageWithURL:URL(model.icon)];
            imgView.contentMode = UIViewContentModeScaleAspectFit;
            [whiteView addSubview:imgView];
            UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(windth*(i%2)+60*displaywidth, 60*displaywidth*(i/2)+10*displaywidth, windth-70*displaywidth, 40*displaywidth) Font:14.0 Text:string(@"  ", model.name) Color:BlaTitColor];
            [whiteView addSubview:nameLab];
            UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(windth*(i%2), 60*displaywidth*(i/2), windth, 60*displaywidth) ImageName:nil Target:self Action:@selector(closeServicesAction:) Title:nil Font:14.0];
            btn.tag = 1000+i;
            btn.backgroundColor = [UIColor clearColor];
            [whiteView addSubview:btn];
            if (i/2 >= 1 && i%2 == 0)
            {
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(windth*(i%2), 60*displaywidth*(i/2), windth*2, 1*displaywidth)];
                lineView.backgroundColor = BacksColor;
                [whiteView addSubview:lineView];
            }
        }
    }
    UIButton *phoneBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, H, SCREEN_WIDTH-30*displaywidth, 60*displaywidth) ImageName:nil Target:self Action:@selector(callPhoneAction) Title:@"Copyright © 2016-2017 专线网 粤ICP备14053933号-3\n用户协议 加盟热线：0755-89301856" Font:9.0];
    phoneBtn.titleLabel.numberOfLines = 2;
    phoneBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [phoneBtn setTitleColor:RGB(87, 107, 149) forState:UIControlStateNormal];
    [moreView addSubview:phoneBtn];
    moreView.frame = CGRectMake(0, HEIGHT, SCREEN_WIDTH, H+60*displaywidth);
    backView.contentSize = CGSizeMake(SCREEN_WIDTH, HEIGHT+H+60*displaywidth);
}

- (void)callPhoneAction
{
    
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",@"075589301856"]]];
    
}

- (void)closeServicesAction:(UIButton *)btn
{
    NSInteger index = btn.tag-1000;
    MoreServiceModel *model = _moreServiAry[index];
    sharedDelegate.tabbarViewController.selectedIndex = 1;
    [sharedDelegate.tabbarViewController.hallVc reloadServices:model.name ServeId:s_str(model.ID)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
